<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格" prop="goodsGg">
        <el-input
          v-model="queryParams.goodsGg"
          placeholder="请输入商品规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属仓库" prop="storeKind">
        <el-select v-model="queryParams.storeKind" placeholder="请选择所属仓库" clearable>
          <el-option
            v-for="dict in dict.type.store_kind"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="商品单位" prop="goodsUnit">
        <el-input
          v-model="queryParams.goodsUnit"
          placeholder="请输入商品单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="goodsPrice">
        <el-input
          v-model="queryParams.goodsPrice"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单件瓶碗数" prop="goodsUnitPiece">
        <el-input
          v-model="queryParams.goodsUnitPiece"
          placeholder="请输入单件瓶碗数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安全库存" prop="goodsSaveStore">
        <el-input
          v-model="queryParams.goodsSaveStore"
          placeholder="请输入安全库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提前预订天数" prop="goodsCheckDay">
        <el-input
          v-model="queryParams.goodsCheckDay"
          placeholder="请输入提前预订天数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column label="商品编码" align="center" prop="goodsCode" />
      <el-table-column label="商品名称" align="center" prop="goodsName"  width="220"/>
      <el-table-column label="今日剩余库存(件)" align="center" prop="goodsStoreRemain" />
      <el-table-column label="预警库存(件)" align="center" prop="goodsStoreRemainYj" >
        <template slot-scope="scope">
          <span style="color: green;" v-if="(scope.row.goodsStoreRemain - scope.row.twoDayDeliveryNum ) > 0"> {{scope.row.goodsStoreRemain - scope.row.twoDayDeliveryNum }} </span>
          <span style="color: red;" v-else> {{scope.row.goodsStoreRemain - scope.row.twoDayDeliveryNum }} </span>

        </template>
      </el-table-column>
      <el-table-column label="所属仓库" align="center" prop="storeKind">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.store_kind" :value="scope.row.storeKind"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="单价" align="center" prop="goodsPrice" />
      <el-table-column label="单件瓶碗数" align="center" prop="goodsUnitPiece" /> -->
      <el-table-column label="今日剩余库存总金额(元)" align="center" prop="goodsTotal" >
        <template slot-scope="scope">
          {{(scope.row.goodsStoreRemain * scope.row.goodsPrice * scope.row.goodsUnitPiece).toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column label="近30日总出库数(件)" align="center" prop="thrDayDeliveryNum" />
      <el-table-column label="近7日总出库数(件)" align="center" prop="twoDayDeliveryNum" />
      <el-table-column label="近7日平均出库数(件)" align="center" prop="twoDayDeliveryPre" >
        <template slot-scope="scope">
         {{(scope.row.twoDayDeliveryNum / 7).toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column label="前日出库数(件)" align="center" prop="oneDayDeliveryNum" />
      <el-table-column label="昨日出库数(件)" align="center" prop="todayDeliveryNum" />

      <el-table-column label="昨/前日环比" align="center" prop="qoq" >
        <template slot-scope="scope">
          <div>
            <span  style="color: green;" v-if="(scope.row.todayDeliveryNum - scope.row.oneDayDeliveryNum)/scope.row.oneDayDeliveryNum > 0">
             <svg width="10" height="10" viewBox="0 0 100 100">
                   <polygon points="50,10 90,90 10,90" fill="green" />
                 </svg> {{(((scope.row.todayDeliveryNum - scope.row.oneDayDeliveryNum)/scope.row.oneDayDeliveryNum) * 100).toFixed(2)}}%
            </span>
            <span  style="color: red;" v-else-if="(scope.row.todayDeliveryNum - scope.row.oneDayDeliveryNum)/scope.row.oneDayDeliveryNum < 0">
              <svg width="10" height="10" viewBox="0 0 100 100">
                    <polygon points="50,100 0,0 100,0" fill="red" />
                  </svg>{{(((scope.row.todayDeliveryNum - scope.row.oneDayDeliveryNum)/scope.row.oneDayDeliveryNum) * 100).toFixed(2)}}%
            </span>
            <span  style="color:orange;" v-else>
              0.00
            </span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="安全库存" align="center" prop="goodsSaveStore" />
      <el-table-column label="提前预订天数" align="center" prop="goodsCheckDay" /> -->

    </el-table>


  </div>
</template>

<script>
import { listCount, getCount, } from "@/api/shop/count";

export default {
  name: "Goods",
  dicts: ['store_kind'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电商商品管理表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsCode: null,        goodsName: null,        goodsGg: null,        goodsUnit: null,        goodsPrice: null,        goodsUnitPiece: null ,
        storeKind: null},
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询电商商品管理列表 */
    getList() {
      this.loading = true;
      listCount(this.queryParams).then(response => {
        this.goodsList = response.data;
        this.goodsList.sort((a,b)=>{
            return b.todayDeliveryNum - a.todayDeliveryNum
        })
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        remark: null,        status: "0",        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null,        goodsCode: null,        goodsName: null,        goodsGg: null,        goodsUnit: null,        goodsPrice: null,        goodsUnitPiece: null ,        storeKind: null     };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/count/export', {
        ...this.queryParams
      }, `count_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
